<template>
    <view class="content">
        <view class="mart-30" v-if="!isEdit">
            <view class="commit-title">钢丝绳信息</view>
            <view class="topContent readBox" v-if="thisData">
                <ropeInfo ref="ropeInfoRef" :isEdit="isEdit"></ropeInfo>
            </view>
        </view>
        <view class="mart-30">
            <u-tabs :activeStyle="{ color: '#409EFF' }" :current="current" :list="tabList" @click="tabClick"></u-tabs>
            <view class="topContent scrollBox" v-show="current == 0">
                <u--form ref="uForm" :model="thisData" :rules="rules" label-width="120" labelAlign="center" :disabled="!isEdit">
                    <view class="commit-title flexTitle marb-10">
                        <text>检测信息</text>
                    </view>
                    <u-form-item label="检测类型" :required="true" borderBottom prop="type" class="relativeCol">
                        <view class="w-100">
                            <EnumSelect :enums="$enums.CheckType" value="value" label="text" :disabled="!isEdit" :id.sync="thisData.type" :name.sync="thisData.typeName" placeholder="请选择检测类型"></EnumSelect>
                        </view>
                    </u-form-item>
                    <u-form-item label="检测单位" :required="true" borderBottom prop="checkUnit" class="relativeCol">
                        <view class="w-100">
                            <u-input v-model="thisData.checkUnit" :disabled="!isEdit" placeholder="请输入检测单位" />
                        </view>
                    </u-form-item>
                    <u-form-item label="检测人员" :required="true" borderBottom prop="checkUserName" class="relativeCol">
                        <view class="w-100">
                            <u-input v-model="thisData.checkUserName" :disabled="!isEdit" placeholder="请输入检测人员"> </u-input>
                        </view>
                    </u-form-item>
                    <u-form-item label="检测日期" :required="true" borderBottom prop="checkDate" class="relativeCol">
                        <view class="w-100">
                            <uni-datetime-picker :disabled="!isEdit" type="date" :end="$gx.FormatDate('YYYY-MM-DD')" v-model="thisData.checkDate" />
                        </view>
                    </u-form-item>

                    <u-form-item label="下次检测日期" :required="thisData.type == $enums.CheckType.定期" borderBottom prop="nextCheckDate">
                        <view class="w-100">
                            <uni-datetime-picker type="date" :disabled="!isEdit" v-model="thisData.nextCheckDate" />
                        </view>
                    </u-form-item>
                    <u-form-item label="是否建议更换">
                        <view class="w-100">
                            <u-radio-group v-model="thisData.isSuggestChange" :disabled="!isEdit">
                                <u-radio :name="false" label="否" class="padr-20"></u-radio>
                                <u-radio :name="true" label="是"></u-radio>
                            </u-radio-group>
                        </view>
                    </u-form-item>
                    <view class="mart-30 commit-title flexTitle marb-10">
                        <text>钢丝绳信息</text>
                    </view>
                    <u-form-item label="钢丝绳编码" :required="true" borderBottom prop="wireropeKeepTypeID">
                        <view class="w-100">
                            <DataSelect class="w-100" :disabled="!ropeEdit" @onChange="ropeChange()" table="UsingWirerope" action="GetSearchs" :filter="{ _Status: 1 }" label="code" value="autoID" :obj.sync="codeObj" :id.sync="thisData.code" :name.sync="thisData.code"> </DataSelect>
                        </view>
                    </u-form-item>
                    <u-form-item label="钢丝绳名称" borderBottom prop="name">
                        <view class="w-100">
                            <u-input v-model="usingWireropeData.name" disabled placeholder="请输入钢丝绳名称"> </u-input>
                        </view>
                    </u-form-item>
                    <u-form-item label="规格型号" borderBottom prop="guiGe">
                        <view class="w-100">
                            <u-input v-model="usingWireropeData.guiGe" disabled placeholder="请输入规格型号"> </u-input>
                        </view>
                    </u-form-item>
                    <u-form-item label="直径" borderBottom prop="diameter">
                        <view class="w-100">
                            <u-input v-model="usingWireropeData.diameter" disabled placeholder="请输入直径"> </u-input>
                        </view>
                    </u-form-item>
                    <u-form-item label="制造单位" borderBottom>
                        <view class="w-100">
                            <DataSelect class="w-100" disabled table="Producer" action="GetSearch" :filter="{ _Type: 2 }" label="name" value="autoID" :id.sync="usingWireropeData.producerID" :name.sync="usingWireropeData.producerName"> </DataSelect>
                        </view>
                    </u-form-item>
                    <u-form-item label="使用单位" borderBottom>
                        <view class="w-100">
                            <DataSelect class="w-100" disabled table="Department" action="GetSearchWithIsDelete" :filter="{ _Type: $enums.Department_Type.矿山 }" label="name" value="autoID" :id.sync="usingWireropeData.departmentID" :name.sync="usingWireropeData.department_Name"></DataSelect>
                        </view>
                    </u-form-item>
                    <u-form-item label="提升机类型" prop="usingWireropeData.hoisterID" borderBottom>
                        <DataSelect class="w-100" disabled table="Hoister" action="GetSearch" label="name" value="autoID" :id.sync="usingWireropeData.hoisterID" :name.sync="usingWireropeData.hoisterName"></DataSelect>
                    </u-form-item>
                    <u-form-item label="提升机用途" prop="usingWireropeData.hoisterUsageID" borderBottom>
                        <DataSelect class="w-100" disabled table="HoisterUsage" action="GetSearch" label="name" value="autoID" :id.sync="usingWireropeData.hoisterUsageID" :name.sync="usingWireropeData.hoisterUsageName"></DataSelect>
                    </u-form-item>
                    <u-form-item label="钢丝绳类型" prop="usingWireropeData.wireropeTypeID" borderBottom>
                        <DataSelect class="w-100" disabled table="WireropeType" action="GetSearch" label="name" value="autoID" :id.sync="usingWireropeData.wireropeTypeID" :name.sync="usingWireropeData.wireropeTypeName"></DataSelect>
                    </u-form-item>
                    <u-form-item label="安装日期" borderBottom prop="installDate">
                        <view class="w-100">
                            <uni-datetime-picker type="date" disabled v-model="usingWireropeData.installDate" />
                            <!-- <u-input v-model="usingWireropeData.installDate" disabled placeholder="请输入安装日期"> </u-input> -->
                        </view>
                    </u-form-item>
                    <u-form-item label="安装长度" borderBottom prop="installLengthNum">
                        <view class="w-100">
                            <u-input v-model="usingWireropeData.installLengthNum" disabled placeholder="请输入安装长度"> </u-input>
                        </view>
                    </u-form-item>
                    <u-form-item label="安全运行时长" borderBottom prop="dayNum">
                        <view class="w-100">
                            <u-input v-model="usingWireropeData.dayNum" disabled placeholder="请输入安全运行时长">
                                <u--text text="天" slot="suffix" type="tips"></u--text>
                            </u-input>
                        </view>
                    </u-form-item>
                    <u-form-item label="上次维保日期" borderBottom prop="doDate">
                        <view class="w-100">
                            <uni-datetime-picker disabled type="date" v-model="usingWireropeData.lastUsingWireropeKeepModel.doDate" />
                        </view>
                    </u-form-item>
                    <u-form-item label="上次检测日期">
                        <view class="w-100">
                            <uni-datetime-picker disabled type="date" v-model="usingWireropeData.lastCheckWireropeModel.checkDate" />
                        </view>
                    </u-form-item>
                </u--form>
            </view>
            <view class="topContent scrollBox" v-show="current != 0">
                <u--form ref="uForm2" :model="curCheckRope" :rules="rules" label-width="140" labelAlign="center" :disabled="!isEdit">
                    <u-form-item label="绳名称" borderBottom prop="companyName" class="relativeCol">
                        <view class="w-100">
                            <u-input v-model="curCheckRope.name" :disabled="!isFirst" placeholder="请输入绳名称" />
                        </view>
                    </u-form-item>
                    <u-form-item label="附件" borderBottom prop="attr_FilesList" class="relativeCol">
                        <view>
                            <u-button type="primary" @click="file_AttrShow(curCheckRope.attr_FilesList)" size="small" :text="isEdit ? '上传' : '查看'"></u-button>
                        </view>
                    </u-form-item>
                    <view class="mart-30 commit-title flexTitle marb-10">
                        <text>断丝</text>
                        <u-button type="primary" @click="onShowDataEntry(1)" size="small" :text="isEdit ? '录入数据' : '查看数据'"></u-button>
                    </view>
                    <u-form-item label="捻距内断丝数" borderBottom prop="dsCnt" class="relativeCol">
                        <view class="w-100">
                            <u-input
                                type="number"
                                :formatter="
                                    (e) => {
                                        return $gx.inputFormatter(e, 0);
                                    }
                                "
                                v-model="curCheckRope.dsCnt"
                                :disabled="!isEdit"
                                placeholder="请输入捻距内断丝数"
                                @change="changeDsCnt()" />
                        </view>
                    </u-form-item>
                    <u-form-item label="位置" borderBottom prop="companyName" class="relativeCol">
                        <view>
                            <u-button type="primary" :icon="curCheckRope.checkWireropeDetailPositionModels && curCheckRope.checkWireropeDetailPositionModels.length ? 'checkmark' : ''" @click="onShowLookPosition()" size="small" :text="isEdit ? '编辑' : '查看'"></u-button>
                        </view>
                    </u-form-item>
                    <u-form-item label="LA" borderBottom prop="dsLa" class="relativeCol">
                        <view class="w-100 infoBox">
                            <u-input type="number" :formatter="$gx.inputFormatter" v-model="curCheckRope.dsLa" :disabled="!isEdit" placeholder="请输入LA">
                                <u--text text="%" slot="suffix" type="tips"></u--text>
                            </u-input>
                            <zb-tooltip :content="usingWireropeData.laRemark" placement="left">
                                <u-icon name="info-circle" v-if="usingWireropeData.laRemark"></u-icon>
                            </zb-tooltip>
                        </view>
                    </u-form-item>
                    <u-form-item label="捻距内新增断丝数" borderBottom prop="dsCntAdd" class="relativeCol">
                        <view class="w-100">
                            <u-input v-model="curCheckRope.dsCntAdd" disabled />
                        </view>
                    </u-form-item>
                    <u-form-item label="新增断丝总数" borderBottom prop="allCntAdd" class="relativeCol">
                        <view class="w-100">
                            <u-input v-model="curCheckRope.allCntAdd" disabled />
                        </view>
                    </u-form-item>
                    <view class="mart-30 commit-title flexTitle marb-10">
                        <text>直径</text>
                        <u-button type="primary" @click="onShowDataEntry(2)" size="small" :text="isEdit ? '录入数据' : '查看数据'"></u-button>
                    </view>
                    <u-form-item label="位置" borderBottom prop="zjPosition" class="relativeCol">
                        <view class="w-100">
                            <u-input type="number" :formatter="$gx.inputFormatter" v-model="curCheckRope.zjPosition" :disabled="!isEdit" placeholder="请输入位置">
                                <u--text text="m" slot="suffix" type="tips"></u--text>
                            </u-input>
                        </view>
                    </u-form-item>
                    <u-form-item label="DIA" borderBottom prop="zjDia" class="relativeCol">
                        <view class="w-100 infoBox">
                            <u-input type="number" :formatter="$gx.inputFormatter" v-model="curCheckRope.zjDia" :disabled="!isEdit" placeholder="请输入DIA">
                                <u--text text="%" slot="suffix" type="tips"></u--text>
                            </u-input>
                            <zb-tooltip :content="usingWireropeData.diaRemark" placement="left">
                                <u-icon name="info-circle" v-if="usingWireropeData.diaRemark"></u-icon>
                            </zb-tooltip>
                        </view>
                    </u-form-item>
                    <view class="mart-30 commit-title flexTitle marb-10">
                        <text>金属截面积损失</text>
                    </view>
                    <u-form-item label="位置" borderBottom prop="jsjmPosition" class="relativeCol">
                        <view class="w-100">
                            <u-input type="number" :formatter="$gx.inputFormatter" v-model="curCheckRope.jsjmPosition" :disabled="!isEdit" placeholder="请输入位置">
                                <u--text text="m" slot="suffix" type="tips"></u--text>
                            </u-input>
                        </view>
                    </u-form-item>
                    <u-form-item label="LMA" borderBottom prop="jsjmLma" class="relativeCol">
                        <view class="w-100 infoBox">
                            <u-input type="number" :formatter="$gx.inputFormatter" v-model="curCheckRope.jsjmLma" :disabled="!isEdit" placeholder="请输入LMA">
                                <u--text text="%" slot="suffix" type="tips"></u--text>
                            </u-input>
                            <zb-tooltip :content="usingWireropeData.lmaRemark" placement="left">
                                <u-icon name="info-circle" v-if="usingWireropeData.lmaRemark"></u-icon>
                            </zb-tooltip>
                        </view>
                    </u-form-item>
                    <view class="mart-30 commit-title flexTitle marb-10">
                        <text>捻距变化</text>
                    </view>
                    <u-form-item label="捻距" borderBottom prop="nianJu" class="relativeCol">
                        <view class="w-100">
                            <u-input type="number" :formatter="$gx.inputFormatter" v-model="curCheckRope.nianJu" :disabled="!isEdit" placeholder="请输入捻距" @input="onNianJuChange">
                                <u--text text="mm" slot="suffix" type="tips"></u--text>
                            </u-input>
                        </view>
                    </u-form-item>
                    <u-form-item label="捻距变化" borderBottom prop="nianJuChange" class="relativeCol">
                        <view class="w-100 infoBox">
                            <u-input v-model="curCheckRope.nianJuChange" :disabled="true" placeholder="请输入捻距变化">
                                <u--text text="%" slot="suffix" type="tips"></u--text>
                            </u-input>
                            <zb-tooltip :content="usingWireropeData.nianJuRemark" placement="left">
                                <u-icon name="info-circle" v-if="usingWireropeData.nianJuRemark"></u-icon>
                            </zb-tooltip>
                        </view>
                    </u-form-item>
                    <view class="mart-30 commit-title flexTitle marb-10">
                        <text>其他</text>
                    </view>
                    <u-form-item label="腐蚀情况" borderBottom prop="othFuShi" class="relativeCol">
                        <view class="w-100">
                            <u-input v-model="curCheckRope.othFuShi" :disabled="!isEdit" placeholder="请输入腐蚀情况" />
                        </view>
                    </u-form-item>
                    <u-form-item label="磨损情况" borderBottom prop="othMoSun" class="relativeCol">
                        <view class="w-100">
                            <u-input v-model="curCheckRope.othMoSun" :disabled="!isEdit" placeholder="请输入磨损情况" />
                        </view>
                    </u-form-item>
                    <u-form-item label="其他损伤" borderBottom prop="othQiTa" class="relativeCol">
                        <view class="w-100">
                            <u-input v-model="curCheckRope.othQiTa" :disabled="!isEdit" placeholder="请输入其他损伤" />
                        </view>
                    </u-form-item>
                    <u-form-item label="综合判定" borderBottom prop="othZongHe" class="relativeCol">
                        <view class="w-100">
                            <u-input v-model="curCheckRope.othZongHe" :disabled="!isEdit" placeholder="请输入综合判定" />
                        </view>
                    </u-form-item>
                </u--form>
            </view>
            <view class="mart-30 marb-30" v-show="isEdit">
                <u-row justify="center">
                    <u-col span="6">
                        <u-button type="primary" @click="submit('检测')">提交</u-button>
                    </u-col>
                </u-row>
            </view>
        </view>
        <u-toast ref="uToast"></u-toast>
        <ImgShow ref="ImgShowRef"></ImgShow>
        <u-popup :show="showUploadFile" mode="center" round="5" duration="0" closeable :closeOnClickOverlay="true" @close="uploadPopClose">
            <view class="uploadContent">
                <UploadImage ref="UploadRef" :disabled="!isEdit" v-model="FilesList" :limit="9" @close="onUploadClose"></UploadImage>
            </view>
        </u-popup>
    </view>
</template>

<script>
export default {
    data() {
        return {
            showUploadFile: false,
            FilesList: [],
            codeObj: {},
            ropeEdit: false,
            thisData: {
                type: this.$enums.CheckType.日常,
                typeName: "日常",
                checkUserName: getApp().globalData.name,
                checkDate: this.$gx.GetDateTime(),
                isSuggestChange: false,
            },
            isEdit: true,
            isFirst: false,
            lastCheckWireropeModel: {},
            usingWireropeData: {
                lastUsingWireropeKeepModel: {},
                lastCheckWireropeModel: {},
            },
            tabList: [
                {
                    name: "基本信息",
                },
            ],
            checkWireropeDetailModels: [],
            curCheckRope: {},
            current: 0,
            rules: {
                type: [
                    {
                        validator: (rule, value, callback) => {
                            if (this.$gx.IsEmpty(value)) {
                                return callback(new Error("请选择检验类型"));
                            } else {
                                callback();
                            }
                        },
                        trigger: ["blur", "change"],
                    },
                ],
                checkUnit: [
                    {
                        required: true,
                        message: "检测单位",
                        trigger: ["blur", "change"],
                    },
                ],
                checkUserName: [
                    {
                        required: true,
                        message: "检测人员",
                        trigger: ["blur", "change"],
                    },
                ],
                checkDate: [
                    {
                        required: true,
                        message: "检测日期",
                        trigger: ["blur", "change"],
                    },
                ],
                code: [
                    {
                        required: true,
                        message: "钢丝绳编码",
                        trigger: ["blur", "change"],
                    },
                ],
                nextCheckDate: [
                    {
                        validator: (rule, value, callback) => {
                            if (this.$gx.IsEmpty(value) && this.thisData.type == this.$enums.CheckType.定期) {
                                return callback(new Error("请选择下次检测日期"));
                            } else {
                                callback();
                            }
                        },
                        trigger: ["blur", "change"],
                    },
                ],
            },

            prevCheckData: {},
        };
    },
    //生命周期-加载
    onLoad: function (option) {
        let that = this;
        // #ifdef APP-NVUE
        const eventChannel = this.$scope.eventChannel; // 兼容APP-NVUE
        // #endif

        // #ifndef APP-NVUE
        const eventChannel = this.getOpenerEventChannel();
        // #endif
        eventChannel.on("editData", function (data) {
            let type = 0;
            that.isEdit = true;
            that.ropeEdit = false;
            if (data.operateType == "新增") {
                type = 1;
                that.ropeEdit = true;
            }
            if (data.operateType == "详情") {
                that.rules = {};
                uni.setNavigationBarTitle({
                    title: "在用绳检测详情",
                });
                (type = 3), (that.isEdit = false);
            }
            that.init(data.data, type);
        });
    },
    onReady() {
        //如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
        this.$nextTick(() => {
            this.$refs.uForm.setRules(this.rules);
        });
    },
    onShow() {
        const that = this;
        uni.$off("siteDataOperReturn");
        uni.$on("siteDataOperReturn", (res) => {
            let { list, position, maxSum, type, wireIndex } = { ...res };
            wireIndex = wireIndex - 1;
            if (list.length) {
                if (type == that.$enums.DataEntry_Type.断丝) {
                    that.checkWireropeDetailModels[wireIndex].dsCnt = maxSum;
                    that.checkWireropeDetailModels[wireIndex].checkWireropeDetailPositionModels = position;
                    that.checkWireropeDetailModels[wireIndex].dsList = list;
                    that.changeDsCnt(wireIndex);
                    that.changeAllDsCnt(wireIndex);
                } else if (type == that.$enums.DataEntry_Type.直径) {
                    that.checkWireropeDetailModels[wireIndex].zjPosition = position;
                    that.checkWireropeDetailModels[wireIndex].zjDia = maxSum;
                    that.checkWireropeDetailModels[wireIndex].zjList = list;
                }
            }
        });
        uni.$off("site2DataOperReturn");
        uni.$on("site2DataOperReturn", (res) => {
            let { list, wireIndex } = { ...res };
            wireIndex = wireIndex - 1;
            if (list.length) {
                that.checkWireropeDetailModels[wireIndex].checkWireropeDetailPositionModels = list;
            }
        });
    },
    methods: {
        onShowDataEntry(type) {
            let data = this.curCheckRope;
            data.installLengthNum = this.usingWireropeData.installLengthNum;
            data.nianJu = this.usingWireropeData.nianJu;
            data.diameter = this.usingWireropeData.diameter;
            let url = "/pages/stateMonitoring/ropeSite";
            const that = this;
            uni.navigateTo({
                url: url,
                success: function (res) {
                    res.eventChannel.emit("ropeData", {
                        data: {
                            name: data.name,
                            data: type == that.$enums.DataEntry_Type.断丝 ? data.dsList : data.zjList,
                            type: type,
                            isEdit: that.isEdit,
                            usingData: data,
                            wireIndex: that.current,
                        },
                    });
                },
            });
        },
        onShowLookPosition() {
            let data = this.curCheckRope;
            data.installLengthNum = this.usingWireropeData.installLengthNum;
            data.nianJu = this.usingWireropeData.nianJu;
            data.diameter = this.usingWireropeData.diameter;
            let url = "/pages/stateMonitoring/ropeSite2";
            const that = this;
            uni.navigateTo({
                url: url,
                success: function (res) {
                    res.eventChannel.emit("ropeData", {
                        data: {
                            name: data.name,
                            data: data.checkWireropeDetailPositionModels,
                            isEdit: that.isEdit,
                            usingData: data,
                            wireIndex: that.current,
                        },
                    });
                },
            });
        },
        tabClick(item) {
            let index = item.index;
            this.current = index;
            if (index != 0) {
                this.curCheckRope = this.checkWireropeDetailModels[index - 1];
            }
        },
        clearAll() {
            this.tabList = [
                {
                    name: "基本信息",
                },
            ];
            this.checkWireropeDetailModels = [];
        },
        ropeChange(type) {
            let rpoeData = this.codeObj;
            if (Object.keys(rpoeData).length < 1) {
                this.setRopeData("");
                this.clearAll();
            } else {
                if (type != "noCallback") {
                    this.clearAll();
                }
                this.setRopeData(rpoeData);
            }
            let { cnt, autoID } = {
                ...rpoeData,
            };

            if (type != "noCallback") {
                this.getUseRopeData(autoID);
            }
            this.getPrevRecord(rpoeData.usingWireropeID);
        },
        setRopeData(data) {
            let hasCol = ["usingWireropeID", "code", "stockID", "name", "guiGe", "diameter", "nianJu", "lengthNum", "cnt", "mpaID", "mpaName", "surfaceStatusID", "surfaceStatusName", "twistingTypeID", "twistingTypeName", "departmentID", "department_Name", "producerID", "producerName", "installDate", "installLengthNum", "dayNum", "hoisterName", "hoisterUsageID", "hoisterUsageName", "wireropeTypeID", "wireropeTypeName", "remark"];
            hasCol.map((x) => {
                if (data == "") {
                    this.$set(this.thisData, x, "");
                } else {
                    this.$set(this.thisData, x, data[x]);
                }
            });
        },
        init(data, type) {
            // type1编辑0添加
            if (data.checkWireropeID) {
                this.thisID = data.checkWireropeID;
                this.$app.CallApi(this.$app.api.CheckWirerope.GetByID(this.thisID), {
                    success: (res) => {
                        this.thisData = res;
                        this.thisData.typeName = this.$enums.CheckType.Get(res.type).text;
                        this.$refs.uForm.clearValidate();
                        //循环绳子过滤类型
                        for (var i = 0; i < res.checkWireropeDetailModels.length; i++) {
                            let item = res.checkWireropeDetailModels[i];
                            item.dsList = item.checkWireropeDetailDataModels.filter((x) => x.type == this.$enums.DataEntry_Type.断丝);
                            item.zjList = item.checkWireropeDetailDataModels.filter((x) => x.type == this.$enums.DataEntry_Type.直径);
                        }
                        this.checkWireropeDetailModels = res.checkWireropeDetailModels;
                        this.getUseRopeData(res.usingWireropeID);
                    },
                });
            }
        },
        getUseRopeData(ropeID) {
            this.$app.CallApi(this.$app.api.UsingWirerope.GetStatusMonitorByID(ropeID), {
                success: (res) => {
                    let dayNum = this.$gx.DateDiff("date", res.installDate, this.$gx.GetDateTime());
                    res.dayNum = dayNum;
                    (res.lastUsingWireropeKeepModel = res.lastUsingWireropeKeepModel || {}), (res.lastCheckWireropeModel = res.lastCheckWireropeModel || {}), (this.usingWireropeData = res);
                    if (!this.isEdit) {
                        for (let i = 1; i <= this.checkWireropeDetailModels.length; i++) {
                            this.tabList.push({
                                name: `${i}号绳`,
                            });
                            let item = { name: null };
                            if (res.lastCheckWireropeModel.checkWireropeDetailModels) {
                                item = res.lastCheckWireropeModel.checkWireropeDetailModels[i - 1];
                            }
                            this.checkWireropeDetailModels[i - 1]["name"] = item.name;
                        }
                        this.$refs.ropeInfoRef.init(res);
                        this.codeObj = res;
                        this.ropeChange("noCallback");
                    } else {
                        this.lastCheckWireropeModel = res.lastCheckWireropeModel || {};
                        this.isFirst = true;
                        if (res.lastCheckWireropeModel && res.lastCheckWireropeModel.checkWireropeDetailModels) {
                            this.isFirst = false;
                        }
                        for (let ind = 1; ind <= res.cnt; ind++) {
                            const that = this;
                            this.tabList.push({
                                name: `${ind}号绳`,
                            });
                            let objData = this.$_.cloneDeep({
                                usingWireropeID: res.usingWireropeID,
                                name: "",
                                dsList: [],
                                zjList: [],
                                checkWireropeDetailModels: [],
                                checkWireropeDetailPositionModels: [],
                            });
                            if (!this.isFirst) {
                                let item = res.lastCheckWireropeModel.checkWireropeDetailModels[ind - 1];
                                objData["name"] = item.name;
                                if (this.checkWireropeDetailModels.length == res.cnt) {
                                    this.checkWireropeDetailModels[ind - 1]["name"] = item.name;
                                } else {
                                    this.checkWireropeDetailModels.push(objData); //几个绳子几条
                                }
                            } else {
                                if (this.checkWireropeDetailModels.length < res.cnt) {
                                    this.checkWireropeDetailModels.push(objData); //几个绳子几条
                                }
                            }
                        }
                        this.getPrevRecord(res.usingWireropeID);
                    }
                },
            });
        },
        submit(type) {
            for (let i = 0; i < this.checkWireropeDetailModels.length; i++) {
                let obj = this.checkWireropeDetailModels[i];
                let sortNo = Number(i) + 1;
                obj.checkWireropeDetailDataModels = [...obj.dsList, ...obj.zjList];
                obj.usingWireropeID = this.thisData.usingWireropeID;
                obj.sortNo = sortNo;
                for (let item in obj) {
                    if (this.$gx.IsEmpty(obj[item]) && item != "attr_Files") {
                        this.$refs.uToast.show({
                            type: "warning",
                            title: "",
                            message: "请完善" + (Number(i) + 1) + "号绳信息！",
                        });
                        return;
                    }
                }
            }

            this.$refs.uForm
                .validate()
                .then((res) => {
                    let apiFun = this.$app.api.CheckWirerope.Insert;
                    if (this.thisID) {
                        apiFun = this.$app.api.CheckWirerope.Check;
                        if (this.$gx.IsEmpty(this.thisData.nextCheckDate)) {
                            this.$u.toast("请选择基本信息下次监测日期！");
                            return;
                        }
                    }
                    if (type == "检测") this.thisData.status = this.$enums.CheckWirerope_Status.已检测;

                    this.thisData.checkWireropeDetailModels = this.checkWireropeDetailModels;

                    this.$app.CallApi(apiFun, {
                        data: this.thisData,
                        success: (res) => {
                            this.$refs.uToast.show({
                                type: "success",
                                title: "",
                                message: "保存成功！",
                                complete: () => {
                                    uni.navigateBack();
                                    uni.$emit("refreshData");
                                },
                            });
                        },
                    });
                })
                .catch((errors) => {});
        },
        //查看
        file_AttrShow(list) {
            this.showUploadFile = true;
            this.FilesList = list;
        },
        uploadPopClose() {
            this.showUploadFile = false;
            console.log("popClose");
            console.log(this.FilesList);
        },
        //捻距内容变化
        onNianJuChange(val) {
            if (!this.isEdit) {
                return;
            }
            let nianJu = this.usingWireropeData.nianJu;
            let nianJuChange = Math.abs(this.$gx.Chu(this.$gx.Jian(val, nianJu), nianJu) * 100).toFixed(2);
            this.$set(this.curCheckRope, "nianJuChange", nianJuChange);
        },

        changeDsCnt() {
            let item = this.curCheckRope;
            let prev_val = 0;
            let prev_item = _.find(this.prevCheckData.checkWireropeDetailModels, function (obj) {
                return obj.name == item.name;
            });
            if (prev_item != undefined) {
                prev_val = prev_item.dsCnt;
            }
            let temp = item.dsCnt == "" ? 0 : parseInt(item.dsCnt);
            item.dsCntAdd = temp - prev_val;
        },
        changeAllDsCnt(index) {
            let that = this;
            if (this.isEdit == true) {
                let item = this.curCheckRope;

                let cur_list = this.checkWireropeDetailModels[index].dsList;
                let cur_sum = this.$_.sumBy(cur_list, function (obj) {
                    return parseInt(obj.num);
                });

                let prev_val = 0;
                let prev_item = this.$_.find(that.prevCheckData.checkWireropeDetailModels, function (obj) {
                    return obj.name == item.name;
                });
                if (prev_item != undefined) {
                    prev_val = _.sumBy(prev_item.checkWireropeDetailDataModels, function (obj) {
                        if (obj.type == that.$enums.DataEntry_Type.断丝) {
                            return obj.num;
                        } else {
                            return 0;
                        }
                    });
                }
                this.curCheckRope.allCntAdd = cur_sum - prev_val;
            }
        },
        getPrevRecord(id) {
            this.$app.CallApi(this.$app.api.CheckWirerope.GetPrevCheckWireropeModel, {
                data: {
                    _UsingWireropeID: id,
                    _CreateDate: this.thisData.createDate,
                },
                success: (res) => {
                    this.prevCheckData = res;
                },
            });
        },

        onUploadClose() {
            this.showUploadFile = false;
            this.curCheckRope.attr_FilesList = this.$gx.DeepCopy(this.FilesList);
        },
    },
};
</script>

<style scoped>
.flexTitle {
    display: flex;
    align-items: center;
}

.flexTitle uni-button {
    width: 150rpx;
    margin: 0;
    margin-left: 30rpx;
}
.infoBox {
    display: flex;
    align-items: center;
    .u-icon {
        padding-left: 6rpx;
    }
}
.uploadContent {
    width: 600rpx;
    padding: 20rpx 20px 20rpx 20rpx;
}
</style>
